package net.qiyuesuo.utils.file;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileTransformUtils {

	/**
	 * 将inputStram转为byte[] 数组
	 * @param input
	 * @return
	 * @throws IOException
	 */
	public static byte[] InputToBytes(InputStream input) throws IOException {
		byte[] bytes = null;
		try (ByteArrayOutputStream output = new ByteArrayOutputStream();) {
			byte[] buffer = new byte[4096];
			int len = 0;
			while ((len = input.read(buffer))!= -1) {
				output.write(buffer, 0, len);
			}
			bytes = output.toByteArray();
		}
		return bytes;
	}
	
	
	/** 
     * 将File文件 转为 byte[] 数组 
     */  
    public static byte[] fileToBytes(File file){  
		byte[] buffer = null;
		try (FileInputStream fis = new FileInputStream(file);
				ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);) {
			byte[] buf = new byte[1000];
			int len;
			while ((len = fis.read(buf)) != -1) {
				bos.write(buf, 0, len);
			}
			buffer = bos.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return buffer;
    }
	
}